package com.xuecheng.learning.listener;

import com.alibaba.fastjson.JSON;
import com.xuecheng.api.learning.model.dto.CourseRecordDTO;
import com.xuecheng.api.order.model.dto.OrdersDTO;
import com.xuecheng.api.order.model.dto.PayResultModel;
import com.xuecheng.learning.service.CourseRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created on 2021/04/19/15:12 周一
 *
 * @author Lantin
 */

@Component
@Slf4j
@RocketMQMessageListener(consumerGroup = "consumer-group-order-pay-main", topic = "${rocketmq.massage.topic}")
public class CourseRecordListener implements RocketMQListener<String> {

    @Autowired
    private CourseRecordService courseRecordService;

    @Override
    public void onMessage(String message) {

        PayResultModel payResultModel = JSON.parseObject(message, PayResultModel.class);

        OrdersDTO order = payResultModel.getOrder();
        log.info("收到订单支付成功消息,开始生成学习记录,订单号:{}", order.getOrderNo());
        //为支付用户创建学习记录
        CourseRecordDTO courseRecordDTO = new CourseRecordDTO();
        courseRecordDTO.setCoursePubId(order.getCoursePubId());
        courseRecordDTO.setUserName(order.getUserName());

        courseRecordService.createCourseRecord(courseRecordDTO);

    }
}
